import { defineStore } from 'pinia';
import request from '@/utils/request';

interface UserInfo {
  id: number;
  username: string;
  avatar?: string;
  isVerified: boolean;
}

interface State {
  userInfo: UserInfo | null;
  version: string;
  loading: boolean;
}

export const store = defineStore('main', {
  state: (): State => ({
    userInfo: null,
    version: localStorage.getItem('app_version') || '',
    loading: false
  }),

  actions: {
    setLoading(loading: boolean) {
      this.loading = loading;
    },

    async checkVersion() {
      try {
        const { version } = await request.get('/version');
        if (version !== this.version) {
          localStorage.setItem('app_version', version);
          window.location.reload();
        }
      } catch (error) {
        console.error('检查版本失败:', error);
      }
    },

    async getUserInfo() {
      try {
        const data = await request.get('/user/info');
        this.userInfo = data;
      } catch (error) {
        this.userInfo = null;
      }
    }
  }
}); 